package com.eqdd.yiqidian.ui.yiqixue.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.eqdd.yiqidian.CoolRefreshRecyclerViewFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LearnCommentTeacherListBean;
import com.eqdd.yiqidian.box.span.MovementMethod.ClickMovementMethod;
import com.eqdd.yiqidian.box.span.TagSpan;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.ui.yiqixue.LearnSearchActivity;
import com.eqdd.yiqidian.ui.yiqixue.info.LearnTeacherInfoActivity;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 18-2-7  下午2:01.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class CommendTeacherFragment extends BaseFragment {
    private CoolRefreshRecyclerViewFragmentCustom dataBinding;
    private SlimAdapterEx<LearnCommentTeacherListBean> slimAdapterEx;
    private String url;

    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_cool_recycler_view, null, false);
    }

    @Override
    protected void initData() {
        if (slimAdapterEx == null) {
            RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
                refresh();
            });
            url = getArguments().getString(Config.URL);
            String lectureGuid = getArguments().getString(Config.LECTURE_GUID);
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            //添加自定义分割线
            DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
            dataBinding.recyclerView.addItemDecoration(divider);
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_114, new SlimInjector<LearnCommentTeacherListBean>() {
                @Override
                public void onInject(LearnCommentTeacherListBean data, IViewInjector injector) {

                    injector.image(R.id.iv_cover, data.getLectureImage())
                            .text(R.id.tv_one, data.getLectureName())
                            .with(R.id.tv_two, view -> {
                                TextView textView = (TextView) view;
                                textView.setText("");
                                String[] split = data.getLectureType().split(",");
                                for (String s : split) {
                                    SpannableString spanString = new SpannableString(s);
                                    TagSpan tagSpan = new TagSpan(Color.GREEN, 10, 10);
                                    tagSpan.setTextClick(text -> {
                                        Bundle bundle = new Bundle();
                                        bundle.putInt(Config.POS, 0);
                                        bundle.putString(Config.FIELD, text.toString());
                                        startActivity(LearnSearchActivity.class, bundle);
                                    });
                                    spanString.setSpan(tagSpan, 0, s.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                    textView.append(spanString);
                                }
                                textView.setMovementMethod(new ClickMovementMethod());
                            })
                            .text(R.id.tv_three, data.getLectureIntroduce())
                            .text(R.id.tv_four, data.getCreateTime());
                }
            }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        Bundle bundle = new Bundle();
                        bundle.putString(Config.GUID,slimAdapterEx.getDataItem(position).getLectureGuid());
                        startActivity(LearnTeacherInfoActivity.class,bundle);
                    });
            dataBinding.coolRefreshView.setRefreshing(true);
        }
    }

    private void refresh() {
        OkGo.<HttpResult<List<LearnCommentTeacherListBean>>>post(HttpConfig.BASE_URL + url)
                .execute(new JsonCallBack<HttpResult<List<LearnCommentTeacherListBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<LearnCommentTeacherListBean>>> response) {
                        HttpResult<List<LearnCommentTeacherListBean>> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }

                    @Override
                    public void onError(Response<HttpResult<List<LearnCommentTeacherListBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);

                    }
                });

    }

    @Override
    protected void setView() {

    }

    @Override
    public void onClick(View v) {

    }
}
